package gavdi.dk.view;



import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


import gavdi.dk.controller.AbsenceAdapter;
import gavdi.dk.controller.Controller;
import gavdi.dk.controller.EventAdapter;
import gavdi.dk.controller.AssignedWBSAdapter;
import gavdi.dk.controller.WBSAdapter;
import gavdi.dk.data.DAOAbsence;
import gavdi.dk.data.DAOCustomer;
import gavdi.dk.data.DAOUser;
import gavdi.dk.data.DAOAssignedWBS;
import gavdi.dk.model.Absence;
import gavdi.dk.model.Customer;
import gavdi.dk.model.User;
import gavdi.dk.view.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class WBSActivity extends Activity {
	
private String whatToShow;
private Bundle bundle;
private LinearLayout ll;
private TextView  tvName;
private TextView  tvWBSorCode;

private String userID;

//private LinearLayout listWBSorCode;
// private LinearLayout listWBSorCode;

ListView listWBSorCode;
DAOCustomer  dCustomer;

DAOAssignedWBS dUserWBS;

DAOAbsence dAbsence;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wbs_layout);
        
  
        initAttributes();
        createLayout();
   	 
        


        
        listWBSorCode.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				ll = (LinearLayout) view;
				
				tvName = (TextView) ll.getChildAt(0);
				tvWBSorCode = (TextView) ll.getChildAt(1);
	

				bundle.putString("name",  tvName.getText().toString());
				bundle.putString("WBSorCode",  tvWBSorCode.getText().toString());
				
				Intent intent = getIntent().putExtras(bundle);
				
	            setResult(RESULT_OK, intent);
				finish();
	
				
			}
		});
        
        
        
			
    }
    
    private void createLayout() {

    	 
    	 if ( whatToShow.equals("myWBS"))
    		 
         {
    		 AssignedWBSAdapter adapter = new AssignedWBSAdapter(this, R.layout.wbs_item_row, dUserWBS.getAllAssignedWBSbyUserID(userID) );
             listWBSorCode.setAdapter(adapter);
 	 
         }
         if ( whatToShow.equals("showAllWBS") )
         {
   

        	 WBSAdapter adapter = new WBSAdapter(this, R.layout.wbs_item_row, dCustomer.getAllCustomers() );
                
              listWBSorCode.setAdapter(adapter);
              
              
         }
         if ( whatToShow.equals("showAllCodes"))
         {
        	
             AbsenceAdapter adapter = new AbsenceAdapter(this, R.layout.absence_item_row, dAbsence.getAllAbsence());
             
             listWBSorCode.setAdapter(adapter);
         }
         
	}

	private void initAttributes(){
		
		dCustomer = new DAOCustomer(getBaseContext());
		dUserWBS = new DAOAssignedWBS(getBaseContext());
		dAbsence = new DAOAbsence(getBaseContext());
		
		bundle = getIntent().getExtras();
		userID = bundle.getString("userID");


        
		listWBSorCode = (ListView)findViewById(R.id.listWBSorCode);
		whatToShow = bundle.getString("WBSorCode");
		
		
    }
	


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.wbs_layout, menu);
        return true;
    }
}
